package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.Dto.CompanyPage;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Company;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.servlet.ModelAndView;

/**
 * 企业基本 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-04-26
 */
@Controller
@RequestMapping("/system/company")
public class CompanyController extends BaseController
{
    private String prefix = "system/company";
	
	@Autowired
	private ICompanyService companyService;
	
	@RequiresPermissions("system:company:view")
	@GetMapping()
	public String company()
	{
	    return prefix + "/company";
	}
	
	/**
	 * 查询企业基本列表
	 */
	@RequiresPermissions("system:company:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Company company)
	{
		startPage();
        List<Company> list = companyService.selectCompanyList(company);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出企业基本列表
	 */
	@RequiresPermissions("system:company:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Company company)
    {
    	List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        return util.exportExcel(list, "company");
    }
	
	/**
	 * 新增企业基本
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存企业基本
	 */
	@RequiresPermissions("system:company:add")
	@Log(title = "企业基本", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Company company)
	{		
		return toAjax(companyService.insertCompany(company));
	}

	/**
	 * 修改企业基本
	 */
	@GetMapping("/edit/{cid}")
	public String edit(@PathVariable("cid") Integer cid, ModelMap mmap)
	{
		Company company = companyService.selectCompanyById(cid);
		mmap.put("company", company);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存企业基本
	 */
	@RequiresPermissions("system:company:edit")
	@Log(title = "企业基本", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Company company)
	{		
		return toAjax(companyService.updateCompany(company));
	}
	
	/**
	 * 删除企业基本
	 */
	@RequiresPermissions("system:company:remove")
	@Log(title = "企业基本", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(companyService.deleteCompanyByIds(ids));
	}


	/**
	 * 查询企业详情
	 */
	@GetMapping("/selectCompanyById")
	public ModelAndView selectCompanyById(int Cid){

		ModelAndView modelAndView= new ModelAndView();

		modelAndView.addObject("company",this.companyService.selectCompanyById(Cid));
		modelAndView.setViewName(prefix+"/companyDetails");
		return modelAndView;
	}
	/**
	 * 搜索页跳转
	 */
	@GetMapping("/search")
	public ModelAndView search(){

		ModelAndView modelAndView= new ModelAndView();
		modelAndView.setViewName(prefix+"/searchpage");
		return modelAndView;
	}

}
